//
//  Helper.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/23.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class Helper: NSObject {
    public class func checkCountryImageName(name:String) -> String {
        let path = Bundle.main.bundlePath
        let plistName:NSString = "countryData.plist"
        let finalPath:NSString = (path as NSString).appendingPathComponent(plistName as String) as NSString
        //        countryArr = NSDictionary(contentsOfFile:finalPath as String)
        var photoName = ""
        let countryArr =  NSArray(contentsOfFile: finalPath as String)
        countryArr?.enumerateObjects({ (dic, index, stop) in
            guard let newDic = dic as? [String:Any] else {
                stop.initialize(to: true)
                return
            }
            guard let detailArray = newDic["details"] as? [[String : Any]] else {
                stop.initialize(to: true)
                return
            }
            (detailArray as NSArray).enumerateObjects({ (smlDic, position, endBool) in
                guard  let newSmlDic = smlDic as? [String:Any] else {
                    endBool.initialize(to: true)
                    return 
                }
                if newSmlDic["countryChineseName"] as? String == name || newSmlDic["countryEnglishName"] as? String == name {
                    photoName = newSmlDic["countryFlagName"] as! String
                    endBool.initialize(to: true)
                    stop.initialize(to: true)
                }
             })
            
       })
        return photoName
    }
    
    /// 根据名字（中文名和英文名都可以）获取国家模型
    public class func getCountryModel(with name: String) -> CountryModel? {
        guard let path = Bundle.main.path(forResource: "countryData.plist", ofType: nil) else {
            return nil
        }
        guard let countrys = NSArray(contentsOfFile: path) as? [[String: Any]] else { return nil }
        var desDict: [String:String] = [:]
        for details in countrys {
            if let detail = details["details"] as? [[String : String]] {
                for country in detail {
                    if country["countryChineseName"] == name || country["countryEnglishName"] == name {
                        desDict = country
                        break
                    }
                }
            }
        }
        let model = CountryModel(JSON: desDict)
        return model
    }
    
    public class  func timeWithTimeIntervalString(timeStr:Int) ->String{
        let timeInterval:TimeInterval = TimeInterval(timeStr/1000)
        
        let date = NSDate(timeIntervalSince1970: timeInterval)
        
        let dateformatter = DateFormatter()
        
        dateformatter.dateFormat = "HH:mm dd/MM" //自定义日期格式
        
        let time = dateformatter.string(from: date as Date)
        return time
    }
    
    public class func timeWithIntervar(timeStr:String) -> Int {
        let datefmatter = DateFormatter()
        datefmatter.dateFormat="yyyy-MM-dd HH:mm:ss"
        
        let date = datefmatter.date(from: timeStr)
        
        let dateStamp:TimeInterval = date!.timeIntervalSince1970
        
        return Int(dateStamp * 1000)
        
    }
    
    public class func checkTimerCode(response:Response?) -> Bool {
        if let data = response?.value as? [String:Any] {
            let status = data["status"] as? Int ?? 0
            if status == 500 {
                return false
            }
        }
        return false
    }
    
}
